package com.deep.bilibili.util;

import com.deep.bilibili.config.FfmpegConfig;
import com.deep.bilibili.config.LiveConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class LiveUtil {
    @Autowired
    LiveConfig liveConfig;

    public static LiveConfig staticLiveConfig = null;

    @PostConstruct
    public void init() {
        staticLiveConfig = liveConfig;
    }

    public static String getRtmpUrl(String streamName) {
        // rtmp://localhost/live/livestream
        String rtmpServer = staticLiveConfig.getRtmp().get("server");
        String rtmpPrefix = staticLiveConfig.getRtmp().get("prefix");
        String liveUrl = "rtmp://" + rtmpServer + "/" + rtmpPrefix + "/" + streamName;
        return liveUrl;
    }

    public static String getHlsUrl(String streamName) {
        // http://localhost:8080/live/livestream.m3u8
        String rtmpServer = staticLiveConfig.getHls().get("server");
        String rtmpPort = staticLiveConfig.getHls().get("port");
        String rtmpPrefix = staticLiveConfig.getHls().get("prefix");
        String rtmpSuffix = staticLiveConfig.getHls().get("suffix");
        String liveUrl = "http://" + rtmpServer  + ":" + rtmpPort +  "/" + rtmpPrefix + "/" + streamName + "/" + streamName + "." + rtmpSuffix;
        return liveUrl;
    }

    public static String getFlvUrl(String streamName) {
        // http://localhost:8080/live/livestream.flv
        String rtmpServer = staticLiveConfig.getFlv().get("server");
        String rtmpPort = staticLiveConfig.getFlv().get("port");
        String rtmpPrefix = staticLiveConfig.getFlv().get("prefix");
        String liveUrl = "http://" + rtmpServer + ":" + rtmpPort + "/" + rtmpPrefix + "/" + streamName;
        return liveUrl;
    }

    public static String getWebrtcUrl(String streamName) {
        // rtmp://localhost/live/livestream
        String rtmpServer = staticLiveConfig.getWebrtc().get("server");
        String rtmpPrefix = staticLiveConfig.getWebrtc().get("prefix");
        String liveUrl = "webrtc://" + rtmpServer + "/" + rtmpPrefix + "/" + streamName;
        return liveUrl;
    }
}
